package net.expectx.gw.service.impl;

import net.expectx.common.base.BaseResult;
import net.expectx.gw.mapper.PermissionUserMapper;
import net.expectx.gw.bean.PermissionUser;
import net.expectx.gw.service.PermissionUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * PermissionUserService实现
 * @author lijian
 * @date 2019/1/18
 */
@Service
public class PermissionUserServiceImpl extends BaseServiceImpl<PermissionUser> implements PermissionUserService {

    private static final Logger LOGGER = LoggerFactory.getLogger(PermissionUserServiceImpl.class);

    @Autowired
    PermissionUserMapper permissionUserMapper;
    @Override
    public BaseResult insertSelective(PermissionUser record) {
        Map<String, Object>map=new HashMap<>(2);
        String msg;
        boolean flag=false;
        List<PermissionUser> permissionUsers = permissionUserMapper.findByAttr("(user_name ='"+record.getUserName()+"' or phone='"+record.getPhone()+"')", null);
        if (permissionUsers.isEmpty()){
            return super.insertSelective(record);
        }else{
            msg = "输入的用户名已存在,请重新输入!";
        }
        return new BaseResult(false, msg, null);
    }

    @Override
    public BaseResult updateByPrimaryKeySelective(PermissionUser record) {
        Map<String, Object>map=new HashMap<>(2);
        String msg;
        boolean flag=false;
        List<PermissionUser> permissionUsers = permissionUserMapper.findByAttr("id not in("+record.getId()+") and (user_name ='"+record.getUserName()+"' or phone ='"+record.getPhone()+"')", null);
        if (permissionUsers.isEmpty()){
            return super.updateByPrimaryKeySelective(record);
        }else{
            msg = "输入的用户名已存在,请重新输入!";
        }
        return new BaseResult(false, msg, null);
    }
}
